package com.xiaobawang.hundouluo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.xiaobawang.hundouluo.service.UserService;
import com.xiaobawang.hundouluo.utils.SharedPreferencesUtil;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userName_et;
    private EditText password_et;
    private CheckBox remember;
    private CheckBox auto;
    private String userName, password, rememberStatus, autoStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //完成login的BroadcastReceiver的注册
        receiverLogin = new LoginReceiver();
        IntentFilter filterLogin = new IntentFilter();
        filterLogin.addAction("com.xiaobawang.hundouluo.receiver.login");
        LoginActivity.this.registerReceiver(receiverLogin, filterLogin);

        findViewById(R.id.activity_login_btn_login).setOnClickListener(this);
        findViewById(R.id.activity_login_btn_cancel).setOnClickListener(this);
        findViewById(R.id.activity_login_btn_register).setOnClickListener(this);
        findViewById(R.id.activity_login_btn_forget).setOnClickListener(this);

        userName_et = findViewById(R.id.activity_login_et_userName);
        password_et = findViewById(R.id.activity_login_et_password);
        remember = findViewById(R.id.activity_login_cb_remember);
        auto = findViewById(R.id.activity_login_cb_auto);

        rememberStatus = SharedPreferencesUtil.read(LoginActivity.this, "userInfo", "remember");
        autoStatus = SharedPreferencesUtil.read(LoginActivity.this, "userInfo", "auto");

        if (rememberStatus.equals("true")) {

            String uname1 = SharedPreferencesUtil.read(LoginActivity.this, "userInfo", "username");
            String pwd1 = SharedPreferencesUtil.read(LoginActivity.this, "userInfo", "password");

            userName_et.setText(uname1);
            password_et.setText(pwd1);
            remember.setChecked(true);

            Log.e("logininfo", "登录前，用户名是:" + uname1 + "密码是：" + pwd1);

            if (autoStatus.equals("true")) {
                Intent intent1 = new Intent(LoginActivity.this, IndexActivity.class);
                intent1.putExtra("userName", userName_et.getText().toString().trim());
                startActivity(intent1);
                finish();
            }

        }

    }

    BroadcastReceiver receiverLogin;

    @Override
    protected void onDestroy() {
        super.onDestroy();

        LoginActivity.this.unregisterReceiver(receiverLogin);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_login_btn_login:

                userName = userName_et.getText().toString().trim();
                password = password_et.getText().toString().trim();

                if (userName.length() == 0 || password.length() == 0) {
                    Toast.makeText(LoginActivity.this, "请输入账号和密码", Toast.LENGTH_LONG).show();
                    return;
                }

                Intent sIntent = new Intent(LoginActivity.this, UserService.class);
                sIntent.putExtra("type", "login");
                sIntent.putExtra("username", userName);
                sIntent.putExtra("password", password);
                startService(sIntent);
                break;

            case R.id.activity_login_btn_cancel:
                finish();
                break;

            case R.id.activity_login_btn_register:
                Intent intentRegister = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intentRegister);
                finish();
                break;

            case R.id.activity_login_btn_forget:
                Intent intentForget = new Intent(LoginActivity.this, UserCheckActivity.class);
                startActivity(intentForget);
                finish();
                break;

        }
    }

    class LoginReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("loginResult");
            Log.e("LoginActivity", result);
            if (result.equalsIgnoreCase("success")) {
                Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_LONG).show();

                if(remember.isChecked()) {
                    SharedPreferencesUtil.write(LoginActivity.this, "userInfo", "remember", "true");
                    if(auto.isChecked()) {
                        SharedPreferencesUtil.write(LoginActivity.this, "userInfo", "auto", "true");

                    } else {
                        SharedPreferencesUtil.write(LoginActivity.this, "userInfo", "auto", "false");

                    }
                } else {
                    SharedPreferencesUtil.write(LoginActivity.this, "userInfo", "remember", "false");

                }

                SharedPreferencesUtil.write(LoginActivity.this, "userInfo", "username", userName);
                SharedPreferencesUtil.write(LoginActivity.this, "userInfo", "password", password);

                String uname2 = SharedPreferencesUtil.read(LoginActivity.this, "userInfo", "username");
                String pwd2 = SharedPreferencesUtil.read(LoginActivity.this, "userInfo", "password");

                Log.e("logininfo", "登录后，用户名是:" + uname2 + "密码是：" + pwd2);

                Intent intent2 = new Intent(LoginActivity.this, IndexActivity.class);
                intent2.putExtra("userName", userName);
                startActivity(intent2);
                finish();

            } else if(result.equalsIgnoreCase("fail")){
                Toast.makeText(LoginActivity.this, "用户名或密码错误!", Toast.LENGTH_LONG).show();
            }
        }

    }

}
